package com.topisv.tms.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.Properties;

import com.topisv.tms.entity.ExpressStyle;

public abstract class FileOperation {
	
	
	public static String reader(ExpressStyle expressStyle) throws IOException {
		StringBuffer tempString = new StringBuffer();
		BufferedReader br = null;
		InputStreamReader isr = null;
		FileInputStream fis = null;
		try {
			String imageSrcPath = getPath();
			imageSrcPath = getExpressStyleImage(imageSrcPath, expressStyle.getStyleImageUrl());
			StringReader sr=new StringReader(expressStyle.getStyleContent());
			br = new BufferedReader(sr);
			String line = null;
			while ((line = br.readLine()) != null && !line.trim().equals("")) {
				line = getImageSrc(line, imageSrcPath);
				if (line != null)
					tempString.append(line).append(
							SystemUtil.getLineSeparator());
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (br != null) {
				br.close();
			}
		}
		return tempString.toString();
	}
	

	/**
	 * 读取文件内容
	 * 
	 * @param path
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static String reader(String realPath, String name,
			String imageSrcName) throws IOException {
		StringBuffer tempString = new StringBuffer();
		BufferedReader br = null;
		InputStreamReader isr = null;
		FileInputStream fis = null;
		try {
			String imageSrcPath = getPath();
			imageSrcPath = getExpressStyleImage(imageSrcPath, imageSrcName);
			fis = new FileInputStream(getFilePath(realPath, name));
			isr = new InputStreamReader(fis, "UTF-8");
			br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null && !line.trim().equals("")) {
				line = getImageSrc(line, imageSrcPath);
				if (line != null)
					tempString.append(line).append(
							SystemUtil.getLineSeparator());
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (br != null) {
				br.close();
			}
		}
		return tempString.toString();
	}
	
	/**
	 * 读取文件内容
	 * 
	 * @param path
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static String reader(String path) throws IOException {
		StringBuffer tempString = new StringBuffer();
		BufferedReader br = null;
		InputStreamReader isr = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);
			isr = new InputStreamReader(fis, "UTF-8");
			br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				tempString.append(line);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (br != null) {
				br.close();
			}

		}
		return tempString.toString();
	}

	/**
	 * 读取文件内容
	 * 
	 * @param path
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static String reader(String path, String name) throws IOException {
		StringBuffer tempString = new StringBuffer();
		BufferedReader br = null;
		InputStreamReader isr = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(getFilePath(path, name));
			isr = new InputStreamReader(fis, "UTF-8");
			br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null && !line.trim().equals("")) {
				tempString.append(line).append(SystemUtil.getLineSeparator());
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (br != null) {
				br.close();
			}

		}
		return tempString.toString();
	}

	private static String getFilePath(String path, String fileName) {
		StringBuffer filePaths = new StringBuffer(path);
		filePaths.append(SystemUtil.getFileSeparator())
				.append(SystemUtil.filePath_WEB_INF)
				.append(SystemUtil.getFileSeparator())
				.append(SystemUtil.fileName_views)
				.append(SystemUtil.getFileSeparator())
				.append(SystemUtil.file_tracking)
				.append(SystemUtil.getFileSeparator()).append(fileName);
		return filePaths.toString();
	}

	/**
	 * 向指定文件写入内容
	 * 
	 * @param path
	 * @param content
	 * @param name
	 * @throws IOException
	 */
	public static void writer(String path, String content, String name)
			throws IOException {
		try {
			FileOutputStream fos = new FileOutputStream(getFilePath(path, name));
			OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
			osw.write(content);
			osw.close();
		} catch (IOException e) {
			throw e;
		}
	}
	
	/**
	 * 将文字转化为变量
	 * 
	 * @param content
	 * @return
	 * @throws IOException 
	 */
	public static String readerStyleContent(String content) throws IOException {
		StringReader sb = new StringReader(content);
		BufferedReader br = new BufferedReader(sb);
		String line = null;
		StringBuffer tempString = new StringBuffer();
		try {
			while ((line = br.readLine()) != null) {
					tempString.append(line);
			}
		} catch (IOException e) {
          throw e;
		}
		return tempString.toString();
	}

	/**
	 * 将文字转化为变量
	 * 
	 * @param content
	 * @return
	 */
	public static String readerContent(String content) {
		StringReader sb = new StringReader(content);
		BufferedReader br = new BufferedReader(sb);
		String line = null;
		StringBuffer tempString = new StringBuffer();
		try {
			while ((line = br.readLine()) != null ) {
				String temp = getContent(line);
				if (temp != null)
					tempString.append(temp).append(
							SystemUtil.getLineSeparator());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tempString.toString();
	}

	private static String getContent(String line) {
		if (line.startsWith("LODOP.ADD_PRINT_TEXTA(")
				|| line.startsWith("LODOP.ADD_PRINT_TEXTA(\"bz\"")) {
			return replace(line);
		} else if (line.trim().equals("")
				|| line.startsWith("LODOP.PRINT_INITA(\"")
				|| line.startsWith("LODOP.PRINT_INITA(")|| line.startsWith("LODOP.PRINT_INIT(")|| line.startsWith("LODOP.PRINT_INIT(\"")) {
			return null;
		} else if (line.startsWith("LODOP.ADD_PRINT_SETUP_BKIMG(\"")) {
			return null;
		} else {
			return line;
		}
	}

	/**
	 * 转化image的属性
	 * 
	 * @param line
	 * @return
	 */
	private static String getImageSrc(String line, String imageNamePath) {
		if (line.startsWith("LODOP.ADD_PRINT_SETUP_BKIMG(\"")) {
			line = line
					.replaceAll("src='(.*?)'", "src='" + imageNamePath + "'");
		} else if (line.trim().equals("")
				|| line.startsWith("LODOP.PRINT_INITA(\"")
				|| line.startsWith("LODOP.PRINT_INITA(")) {
			return null;
		}

		return line;
	}

	private static String replace(String content) {

		int length = content.length();
		int first = content.indexOf("\"");
		int end = content.lastIndexOf(",");
		int i = first + 1;
		int j = end + 2;

		StringBuffer firstTemp = new StringBuffer();
		while (i < length && content.charAt(i) != '"') {
			firstTemp.append(content.charAt(i));
			i++;
		}
		StringBuffer endTemp = new StringBuffer();
		while (i < length && content.charAt(j) != '"') {
			endTemp.append(content.charAt(j));
			j++;
		}

		return content.replace(endTemp.toString(), "${" + firstTemp.toString()
				+ "}");
	}

	/**
	 * 根据根路径与图片名称返回图片绝对地址
	 * 
	 * @param path
	 * @param imageSrcName
	 * @return
	 */
	public static String getExpressStyleImage(String path, String imageSrcName) {
		StringBuffer imageSrc = new StringBuffer(path);
		imageSrc.append(SystemUtil.file_static).append("/")
				.append(SystemUtil.file_uploadImage).append("/")
				.append(SystemUtil.file_exepress_style).append("/")
				.append(imageSrcName);
		return imageSrc.toString();
	}

	public static String getPath() throws IOException {
		Properties properties = InitProperties.initWeService();
		String address = properties.getProperty("address");
		if (address.endsWith("/")) {
			return address;
		}
		return address + "/";
	}
}
